/**
 * @file tablevieweventfilterchain.cpp
 * @author huangxb
 * @email 18228033207@163.com
 * @date 2025-07-08
 * @brief 表格视图事件处理类
 */
#include "tablevieweventfilter.h"
#include <QTableView>
#include <QEvent>
#include <QMouseEvent>

CClearSelectionEventFilter::CClearSelectionEventFilter(QObject *parent)
    : QObject{parent}
{
    m_pOwner = dynamic_cast<QTableView*>(parent);
}

CClearSelectionEventFilter::CClearSelectionEventFilter(QTableView *pOwner, QObject *parent)
    : QObject{parent}
{
    m_pOwner = pOwner;
}

bool CClearSelectionEventFilter::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() != QEvent::MouseButtonPress)
    {
        return false;
    }
    if(!m_pOwner)
    {
        return false;
    }
    // 清空选中
    auto pMouseEvent = dynamic_cast<QMouseEvent*>(event);
    if (pMouseEvent->buttons().testFlag(Qt::LeftButton))
    {
        QModelIndex index = m_pOwner->indexAt(pMouseEvent->pos());
        if(!index.isValid())
        {
            m_pOwner->setCurrentIndex(QModelIndex());
            m_pOwner->selectionModel()->clearSelection();
        }
    }
    return false;
}
